import React from 'react';
import { IG6GraphEvent, GraphinContext } from '@antv/graphin';

interface INodeClickProps {
  onNodeClick?: (data: any) => void;
}

const RzNodeClick = (props: INodeClickProps) => {
  const { onNodeClick } = props;
  const { graph, apis } = React.useContext(GraphinContext);
  React.useEffect(() => {
    const handleClick = (evt: IG6GraphEvent) => {
      const node: any = evt.item;
      const model = node.getModel();
      //   apis.focusNodeById(model.id);
      onNodeClick && onNodeClick({ id: model.id, data: model.data });
    };
    // 每次点击聚焦到点击节点上
    graph.on('node:click', handleClick);
    return () => {
      graph.off('node:click', handleClick);
    };
  }, []);
  return null;
};

export default RzNodeClick;
